package com.csun.cmny.provider.service.impl;

import com.csun.cmny.base.dto.LoginAuthDto;
import com.csun.cmny.base.enums.ErrorCodeEnum;
import com.csun.cmny.core.support.BaseService;
import com.csun.cmny.provider.dao.OmcRechargeRecordMapper;
import com.csun.cmny.provider.exceptions.OmcBizException;
import com.csun.cmny.provider.model.constant.PointCardAccountConstant;
import com.csun.cmny.provider.model.domain.OmcPointCardAccount;
import com.csun.cmny.provider.model.domain.OmcRechargeRecord;
import com.csun.cmny.provider.model.dto.RechargeDto;
import com.csun.cmny.provider.model.dto.RechargeQueryDto;
import com.csun.cmny.provider.model.vo.DeviceReqVo;
import com.csun.cmny.provider.model.vo.RechargeVO;
import com.csun.cmny.provider.service.DmcRpcService;
import com.csun.cmny.provider.service.OmcPointCardAccountService;
import com.csun.cmny.provider.service.OmcRechargeRecordService;
import com.csun.cmny.provider.utils.DateUtil;
import com.csun.cmny.provider.utils.SerialNumberUtil;
import com.csun.cmny.util.PublicUtil;
import com.csun.cmny.util.RedisKeyUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

@Service
@Slf4j
@Transactional
public class OmcRechargeRecordServiceImpl extends BaseService<OmcRechargeRecord> implements OmcRechargeRecordService {

    @Resource
    private OmcRechargeRecordMapper omcRechargeRecordMapper;
    @Resource
    private OmcPointCardAccountService omcPointCardAccountService;
    @Resource
    private SerialNumberUtil serialNumberUtil;
    @Resource
    private DmcRpcService dmcRpcService;


    @Override
    public int add(RechargeDto rechargeDto, LoginAuthDto loginAuthDto) {

        OmcPointCardAccount omcPointCardAccount = omcPointCardAccountService.selectByKey(rechargeDto.getDeviceId());

        if (PublicUtil.isEmpty(omcPointCardAccount)) {
            throw new OmcBizException(ErrorCodeEnum.OMC10080002);
        }
        if (!omcPointCardAccount.getStatus().equals(PointCardAccountConstant.INUSE_CARD_STATUS)) {
            throw new OmcBizException(ErrorCodeEnum.OMC10080003);
        }
        omcPointCardAccount.setTotal(omcPointCardAccount.getTotal().add(rechargeDto.getNumber()));
        omcPointCardAccount.setRemainder(omcPointCardAccount.getRemainder().add(rechargeDto.getNumber()));
        int update = omcPointCardAccountService.update(omcPointCardAccount);

        OmcRechargeRecord omcRechargeRecord = new OmcRechargeRecord();
        BeanUtils.copyProperties(rechargeDto, omcRechargeRecord);
        omcRechargeRecord.setSerialNumber(serialNumberUtil.getSerialNumberString());
        omcRechargeRecord.setUpdateInfo(loginAuthDto);

        return omcRechargeRecordMapper.insertSelective(omcRechargeRecord);
    }

    @Override
    public PageInfo<RechargeVO> listPage(RechargeQueryDto rechargeQueryDto) {
        PageHelper.startPage(rechargeQueryDto.getPageNum(),rechargeQueryDto.getPageSize());
        List<RechargeVO> voList = omcRechargeRecordMapper.listPage(rechargeQueryDto);
        List<DeviceReqVo> deviceReqVoList = dmcRpcService.listDevice(Lists.newArrayList(voList.stream().map(v -> v.getDeviceId()).collect(Collectors.toList())));
        if(deviceReqVoList.size() > 0){
            for(RechargeVO rechargeVO : voList){
                for (DeviceReqVo deviceReqVo : deviceReqVoList){
                    if(rechargeVO.getDeviceId().equals(deviceReqVo.getDeviceId())){
                        rechargeVO.setDeviceNo(deviceReqVo.getDeviceNo());
                    }
                }
            }
        }
        return new PageInfo<>(voList);
    }

}
